package Buildings.net.server.sequental;
import Buildings.dwelling.Dwelling;
import Buildings.dwelling.hotel.Hotel;
import Buildings.office.*;
import Errors.*;
import Interfaces.Building;
import java.io.*;
import java.net.*;
//import java.util.Random;

public class SerialServer {
    private static final int port=4500;
    private static double getCash(Building b)throws BuildingUnderArrestException{
        int value=(int)(Math.random()*10);
//        Random arest=new Random();
//        int value=arest.nextInt(10);
        if(value==1) throw new BuildingUnderArrestException();
        double s=0;
        int m[]=new int[]{1};
        Dwelling d=new Dwelling(1,m);
        OfficeBuilding o=new OfficeBuilding(1,m);
        Hotel h=new Hotel(1,m);
        if(b.getClass()==d.getClass()){
            s=b.getHowMuchAreaInBuilding()*1000;
        }
        if(b.getClass()==o.getClass()){
            s=b.getHowMuchAreaInBuilding()*1500;
        }
        if(b.getClass()==h.getClass()){
            s=b.getHowMuchAreaInBuilding()*2000;
        }
        return s;
    }
    public static void main(String []arg){
        ServerSocket mySocket=null;
        Socket client=null;
        ObjectInputStream readObj=null;
        ObjectOutputStream writeCash=null;
        System.out.println("Серверное приложение - сериализация!");
        try{
            mySocket=new ServerSocket(port);
            client=mySocket.accept();
            writeCash=new ObjectOutputStream(client.getOutputStream());
            readObj=new ObjectInputStream(new BufferedInputStream(client.getInputStream()));
        }catch(IOException e){
           System.out.println("Ошибка создания-подключения к серверу!");
           System.exit(1);
        }
        System.out.println("Подключено!");
        
        Building example=null;
        Integer end=new Integer(-1);
        Object readed;
        double cash=0;
        do{
            try{
                readed=readObj.readObject();
                if(readed.getClass()==end.getClass()){
                    if(((Integer)readed).intValue()==end.intValue()){
                        System.out.println("Считана метка завершения");
                        break;
                    }
                }
                example=(Building)readed;
                cash=getCash(example);
                System.out.println("Общая цена здания "+cash);
                Double g=new Double(cash);
                writeCash.writeObject(g);
            }catch (BuildingUnderArrestException e){
                try{
                writeCash.writeObject(e);
                System.out.println("Арестован");
                }catch(IOException x){
                    
                }
            }catch (IOException e) {
                System.out.println("Ошибка ввода-вывода ");
                break;
            }catch (ClassNotFoundException ex) {
            System.out.println("Неизвестный класс ");
            }
        }while(true);
        
        try{ 
            readObj.close();
            writeCash.close();
            mySocket.close();
            client.close();
            System.out.println("Сокеты закрыты ");
        }catch (IOException nx){
           System.out.println("Ошибка закрытия сокетов и потоков данных");
        } 
    }
}
